package com.yanqu.road.server.manger.kowloon.battle.role.bullet;

import com.yanqu.road.entity.kowloon.KowloonNpcAttributeData;
import com.yanqu.road.entity.kowloon.common.KowloonBuffParam;
import com.yanqu.road.entity.kowloon.enums.battle.eKowloonBulletType;
import com.yanqu.road.server.manger.kowloon.battle.KowloonBattleFunc;
import com.yanqu.road.server.manger.kowloon.battle.KowloonGame;
import com.yanqu.road.entity.kowloon.common.Vec2;
import com.yanqu.road.server.manger.kowloon.battle.role.KowloonRole;
import com.yanqu.road.server.manger.kowloon.battle.role.enemy.KowloonEnemy;
import com.yanqu.road.server.manger.kowloon.battle.role.hero.KowloonHero;

import java.util.ArrayList;
import java.util.List;

public abstract class KowloonBullet extends KowloonRole {

    public List<Integer> enemyIds = new ArrayList<>();

    public KowloonHero shooter;

    public eKowloonBulletType bulletType;

    public double angle;

    public KowloonEnemy target;

    public KowloonBuffParam buffParam;

    public boolean isDestroyed = false;

    public KowloonBullet(KowloonGame game) {
        super(game);
    }

    public static KowloonBullet addBullet(KowloonGame game, eKowloonBulletType bulletType, KowloonHero shooter,
            KowloonEnemy target, double degree) {
        KowloonBullet bullet = game.getBullet(bulletType);
        bullet.setBulletInfo(bulletType, shooter, target, degree);
        bullet.init();
        game.addBullet(bullet);
        return bullet;
    }

    public void setBulletInfo(eKowloonBulletType bulletType, KowloonHero shooter, KowloonEnemy target, double degree) {
        this.bulletType = bulletType;
        this.shooter = shooter;
        this.target = target;
        this.pos = new Vec2(shooter.pos.getX(), shooter.pos.getY());
        KowloonNpcAttributeData property = shooter.data.getProperty();
        this.isDestroyed = false;
        this.enemyIds = new ArrayList<>();
        switch (this.bulletType) {
            case Apple:
            case Fish:
            case Inkfish:
            case Thunder:
            case Football:
            case Bee: {
                // 直线类型的子弹
                double speed = property.getMoveSpeed();
                double angle = Math.PI * degree / 180;
                this.speed.setX(speed * Math.cos(angle));
                this.speed.setY(speed * Math.sin(angle));
                this.angle = angle;
                switch (this.bulletType) {
                    case Fish:
                        this.buffParam = property.getSlowBuff();
                        break;
                    case Inkfish:
                        this.buffParam = property.getFreezeBuff();
                        break;
                    case Football:
                        this.buffParam = property.getBeatBackBuff();
                        break;
                    case Thunder:
                        this.buffParam = property.getThunderBuff();
                        break;
                }
            }
                break;
            case Spear: {
                this.angle = 0;
                this.buffParam = property.getBleedingBuff();
            }
                break;
            case Amulet:
                this.buffParam = property.getVertigoBuff();
                break;
        }
        this.updatePos(0);
    }

    public abstract void init();

    /** 获取被击中的敌人 */
    public KowloonEnemy getHitEnemy() {
        for (KowloonEnemy enemy : game.getEnemys()) {
            if (enemy.isAlive()) {
                if (enemy.getHurtRect().contains(this.pos)) {
                    if (!this.enemyIds.contains(enemy.id)) {
                        return enemy;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取附近的敌人
     * 
     */
    public KowloonEnemy getNearEnemy(double distance) {
        KowloonEnemy resultEnemy = null;
        for (KowloonEnemy enemy : game.getEnemys()) {
            if (enemy.isAlive()) {
                double dis = game.distance(enemy.pos, this.pos);
                if (dis <= distance) {
                    if (!this.enemyIds.contains(enemy.id)) {
                        resultEnemy = enemy;
                        distance = dis;
                    }
                }
            }
        }
        return resultEnemy;
    }

    /** 获取附近的敌人们 */
    public List<KowloonEnemy> getNearEnemys(double distance) {
        List<KowloonEnemy> resultEnemys = new ArrayList<>();
        List<KowloonEnemy> enemys = game.getEnemys();
        for (KowloonEnemy enemy : enemys) {
            if (enemy.isAlive()) {
                double dis = game.distance(enemy.pos, this.pos);
                if (dis <= distance) {
                    if (!this.enemyIds.contains(enemy.id)) {
                        resultEnemys.add(enemy);
                    }
                }
            }
        }
        return resultEnemys;
    }

    /** 检查是否飞到场景外了，如果在场景外就销毁 */
    public void checkOutOfScene() {
        if (!this.isDestroyed && (this.pos.getY() >= 2000 || this.pos.getY() < -500)) {
            this.destroy();
        }
    }

    /** 销毁 */
    public void destroy() {
        this.isDestroyed = true;
        this.recycle();
    }

    public void recycle() {
        game.recycleBullet(this);
    }

    public eKowloonBulletType getBulletType() {
        return bulletType;
    }

    public KowloonBuffParam getBuffParam() {
        return buffParam;
    }

    public List<Integer> getEnemyIds() {
        return enemyIds;
    }

    public KowloonHero getShooter() {
        return shooter;
    }

    public double getAngle() {
        return angle;
    }

    public KowloonEnemy getTarget() {
        return target;
    }
}
